package com.ruoyi.web.service.impl;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.DesensitizedUtil;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.db.domain.MemberExtInfo;
import com.ruoyi.db.domain.MemberInfo;
import com.ruoyi.market.enums.SpecialEnum;
import com.ruoyi.member.service.IMemberExtInfoService;
import com.ruoyi.member.service.IMemberInfoService;
import com.ruoyi.web.service.IMemberInfoHandleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class MemberInfoHandleServiceImpl implements IMemberInfoHandleService {

    @Autowired
    IMemberInfoService memberInfoService;
    @Autowired
    IMemberExtInfoService memberExtInfoService;

    @Override
    public JSONObject getMemberInfoDetail(Long uid){

        MemberInfo memberInfo = memberInfoService.selectMemberInfoByUid(String.valueOf(uid));
        MemberExtInfo memberExtInfo = memberExtInfoService.selectMemberExtInfoByUid(Long.valueOf(uid));

        if (ObjectUtil.isEmpty(memberExtInfo)){
            memberExtInfo = new MemberExtInfo();
        }

        JSONObject baseInfo = new JSONObject();
        baseInfo.put("uid", String.valueOf(memberInfo.getUid()));
        baseInfo.put("mobile", memberInfo.getMobile());
        baseInfo.put("mobileDe", DesensitizedUtil.mobilePhone(memberInfo.getMobile()));

        baseInfo.put("channelName", memberInfo.getChannelName());
        baseInfo.put("reqIp", memberInfo.getReqIp());
        baseInfo.put("createTime", DateUtil.format(memberInfo.getCreateTime(), DatePattern.NORM_DATETIME_PATTERN));

        baseInfo.put("authFlag", ObjectUtil.equals(memberInfo.getExtInfoFlag(), Long.valueOf("1"))?"已实名":"未实名");
        baseInfo.put("username", memberInfo.getUsername());
        baseInfo.put("idCard", DesensitizedUtil.idCardNum(memberInfo.getIdCard(), 3,3));

        baseInfo.put("province", memberExtInfo.getProvince());
        baseInfo.put("city", memberExtInfo.getCity());

        baseInfo.put("age", memberExtInfo.getAge());
        baseInfo.put("sex", ObjectUtil.isNotEmpty(memberExtInfo.getSex())?(memberExtInfo.getSex()==1?"男":"女"):"");

        JSONObject extInfo = new JSONObject();

        extInfo.put("loanAmount", SpecialEnum.LoanAmount.getNameByValue(memberExtInfo.getLoanAmount()));
        extInfo.put("loanTime", SpecialEnum.LoanTime.getNameByValue(memberExtInfo.getLoanTime()));
        extInfo.put("zhimaScore", SpecialEnum.ZhimaScore.getNameByValue(memberExtInfo.getZhimaScore()));
        extInfo.put("gjj", SpecialEnum.Gjj.getNameByValue(memberExtInfo.getGjj()));
        extInfo.put("salary", SpecialEnum.Salary.getNameByValue(memberExtInfo.getSalary()));

        extInfo.put("house", SpecialEnum.House.getNameByValue(memberExtInfo.getHouse()));
        extInfo.put("job", SpecialEnum.Job.getNameByValue(memberExtInfo.getJob()));
        extInfo.put("car", SpecialEnum.Car.getNameByValue(memberExtInfo.getCar()));
        extInfo.put("xueli", SpecialEnum.XueLi.getNameByValue(memberExtInfo.getXueli()));
        extInfo.put("purpose", SpecialEnum.Purpose.getNameByValue(memberExtInfo.getPurpose()));
        extInfo.put("yuqi", SpecialEnum.Yuqi.getNameByValue(memberExtInfo.getYuqi()));
        extInfo.put("enterpriseOwner", SpecialEnum.EnterpriseOwner.getNameByValue(memberExtInfo.getEnterpriseOwner()));

        extInfo.put("shebao", SpecialEnum.SheBao.getNameByValue(memberExtInfo.getShebao()));
        extInfo.put("baodan", SpecialEnum.BaoDan.getNameByValue(memberExtInfo.getBaodan()));

        extInfo.put("appName", memberExtInfo.getAppName());

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("baseInfo", baseInfo);
        jsonObject.put("extInfo", extInfo);

        return jsonObject;
    }

}
